<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- .wrap>button{新增}+ul.list>li*6{$$} -->
    <div class="wrap">
        <button class="btn">替换</button>
        <ul class="list">
            <li>01</li>
            <li>02</li>
            <li class="mid">03</li>
            <li>04</li>
            <li>05</li>
            <li>06</li>
        </ul>
    </div>
</body>
<script>
    var btn1 = document.getElementsByClassName("btn")[0];
    var btn2 = document.getElementsByClassName("btn")[1];
    var list = document.getElementsByClassName("list")[0];
    var mid = document.getElementsByClassName("mid")[0];

    // 元素替换  =>  父元素统筹
    // parent.removeChild(newNode,oldNode)   => 喜新厌旧   用一个节点(新的/页面中已存在) 替换 另一个节点(页面中已存在的)

    // 点击按钮 删新增一个li 替换mid
    btn1.onclick = function () {
        if (mid) {
            var li = document.createElement("li");
            console.log(li);

            li.innerText = "我是新增的li";  //新增内容
            li.setAttribute("class", "newLi"); // 绑定属性
            li.style.backgroundColor = randColor(); //修改样式
            li.onclick = function () {
                console.log("我是新增的li");
            }

            list.replaceChild(li, mid);
            mid = null;
        }
    }


    // 颜色表示方式
    // 1. 颜色单词  red green blue
    // 2. 16进制   #ffffff   16(0 1 2 3 4 5 6 7 8 9 a b c d e f)
    // 3. ragb     rgba(255,255,255,1);
    function randColor() {
        var str = "0123456789abcdef";
        var colorStr = "#"
        for (var i = 0; i < 6; i++) {
            // var index = Math.round(Math.random() * (str.length - 1));
            var index = Math.floor(Math.random() * str.length);
            var char = str.charAt(index);
            colorStr += char;
        }
        return colorStr;
    }




</script>

</html>